package com.liuhm.bfs;

import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 修改结构
 *
 * @author liuhaomin
 * @date 2020/12/30
 */
@Data
public class Dag2 implements Serializable {

    private static final long serialVersionUID = 3247378463655231751L;
    private Map<String, Vertex> map;

    public Dag2() {
        this.map = new HashMap<>();
    }

    public void addEdge(String from, String to){
        Vertex vFrom= map.get(from)!=null?map.get(from):new Vertex(from,from);
        Vertex vTo = map.get(to)!=null?map.get(to):new Vertex(to,to);
        vFrom.getChildren().add(vTo);
        vTo.getParents().add(vFrom);
        map.put(from,vFrom);
        map.put(to,vTo);
    }

    public List<Vertex> getRoot(){
        List<Vertex> result = new ArrayList<>();
        this.map.forEach((k,v)->{
            if(v.getParents().size()==0){
                result.add(v);
            }
        });
        return result;
    }
}

